import { Base } from "./base";

export class Coclass extends Base {
  /**
   *
   * @param name
   * @param pageNum
   * @param pageSize
   * @returns {Promise<void>}
   */
  // eslint-disable-next-line camelcase
  async list({ fid, pageNum = 1, pageSize = 10 }) {
    const { data = {} } = await this.$http("post", "/api/coclass", {
      fid,
      pageNum,
      pageSize
    });
    data.data.map(r => {
      r.has_children = r.has_children > 0;
    });
    return data;
  }

  /**
   *
   * @param id
   * @returns {Promise<boolean>}
   */
  async del(id) {
    const { errno } = await this.$http("post", "/api/coclass/del", { id });
    return errno === 0;
  }

  /**
   *
   * @param name
   * @param key
   * @param value
   * @param fid
   * @param sort
   * @returns {Promise<void>}
   */
  async add({ name, fid, key, value, sort }) {
    const { errno } = await this.$http("post", "/api/coclass/add", {
      name,
      key,
      value,
      fid,
      sort
    });
    return errno === 0;
  }

  /**
   *
   * @param id
   * @param name
   * @param key
   * @param value
   * @param type
   * @returns {Promise<void>}
   */
  // eslint-disable-next-line camelcase
  async update({ id, name, key, value, type, has_children }) {
    const { errno } = await this.$http("post", "/api/coclass/update", {
      id,
      name,
      key,
      value,
      type,
      has_children
    });
    return errno === 0;
  }

  /**
   *
   * @param id
   * @param type
   * @returns {Promise<void>}
   */
  async detail(id, type = "detail") {
    const { data } = await this.$http("post", "/api/coclass/detail", {
      id,
      type
    });
    return data;
  }
}
